package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {
	static String driver = "com.mysql.jdbc.Driver";
	static String url = "jdbc:mysql://127.0.0.1:3306/hero?characterEncoding=UTF-8";
	static String user = "root";
	static String pwd = "root";
	static Statement s;
	static Connection c;

	/**
	 * JDBC连接数据库
	 */
	public static void exeSql() {
		try {
			Class.forName(driver);
			c = DriverManager.getConnection(url, user, pwd);
			s = c.createStatement();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 判断注册是否成功
	 * 
	 * @return
	 */
	public static boolean regJDBC() {
		exeSql();
		boolean regflag = false;
		// 插入语句
		String sql = SqlExe.sqlQuery(1);
		// 执行注册
		int result = 0;
		try {
			result = s.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result > 0) {
			regflag = true;
		}
		return regflag;
	}

	/**
	 * 判断登录是否成功
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static boolean logJDBC() {
		exeSql();
		boolean logflag = false;
		// 查询用户登录信息
		String $sql = SqlExe.sqlQuery(3);
		// 执行登录
		try {
			ResultSet rs = s.executeQuery($sql);
			if (rs.next()) {
				logflag = true;
			}else {
				logflag = false;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return logflag;
	}
	
	/**
	 * 查询会员卡号
	 */
	public static int getCard() {
		exeSql();
		int card = 0;
		// 插入语句
		String $$sql = SqlExe.sqlQuery(2);
		// 执行注册
		try {
			ResultSet rs = s.executeQuery($$sql);
			while (rs.next()) {
				card = rs.getInt("vipcard");	
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return card;
		
	}
	/**
	 * 关闭流
	 */
	public static void close() {
		exeSql();
		if (s != null) {
			try {
				s.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (c != null) {
			try {
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
